package com.sf.service.impl;


import com.sf.domain.Product;
import com.sf.mapper.ProductMapper;
import com.sf.qo.PageResult;
import com.sf.qo.QueryObject;
import com.sf.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    private Integer totalCount;

    @PostConstruct
    private void init() {
        System.out.println("ValueObject: PageResult init");
        List<Product> products = productMapper.selectAll();
        totalCount = products.size();
    }

    @Override
    public PageResult selectByPage(QueryObject queryObject) {
        return new PageResult(productMapper.selectByPage(queryObject), queryObject.getCurrentPage(), queryObject.getPageSize(), totalCount);
    }

    @Override
    public Product selectById(Long id) {
        Product product = productMapper.selectById(id);
        return product;
    }

    @Override
    public void save(Product product) {
        productMapper.save(product);
        totalCount += 1;
    }

    @Override
    public void update(Product product) {
        productMapper.update(product);
    }

}
